package com.haikesi.api.devicetype.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haikesi.api.base.Constants;
import com.haikesi.api.base.Type;
import com.haikesi.api.base.User;
import com.haikesi.api.devicetype.dao.DeviceTypeDao;
import com.haikesi.api.devicetype.domain.po.DeviceTypePO;
import com.haikesi.api.devicetype.domain.vo.DeviceTypeSearchVO;
import com.haikesi.api.devicetype.domain.vo.DeviceTypeUpdateAlisaVO;
import com.haikesi.api.devicetype.domain.vo.DeviceTypeVO;
import com.haikesi.api.devicetype.service.IDeviceTypeService;
import com.haikesi.api.typetemplate.dao.TypeTemplateMapper;
import com.haikesi.api.typetemplate.domain.po.TypeTemplatePO;
import com.haikesi.exception.BusinessException;
import com.haikesi.util.ApcosApiUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class DeviceTypeServiceImpl implements IDeviceTypeService {

    @Autowired
    DeviceTypeDao dao;
    @Autowired
    TypeTemplateMapper tempDao;

    @Override
    public List<DeviceTypeVO> getList(DeviceTypeSearchVO searchVO) {
//        DeviceTypePO deviceTypePO = new DeviceTypePO();
//        BeanUtils.copyProperties(searchVO, deviceTypePO);
//        Example example = new Example(DeviceTypePO.class);
//        List<DeviceTypePO> deviceTypePOS = dao.selectByExample(example);
        List<DeviceTypeVO> list = dao.getList(searchVO);
        return list;
    }

    @Override
    public void updateType(String deviceTypes) {
        //本地数据库设备类型码list
        List<String> productCodeList = new ArrayList<>();
        /*//获取本地数据库设备类型码list
        DeviceTypeSearchVO deviceTypeSearchVO = new DeviceTypeSearchVO();
        deviceTypeSearchVO.setPageSize(100);
        List<DeviceTypeVO> list = this.getList(deviceTypeSearchVO);
        for (int i = 0; i < list.size(); i++) {
            //将本地类型码添加到list
            productCodeList.add(list.get(i).getProductCode());
        }*/
//        Example example = new Example(ExcutionPlanPO.class);
//        example.and().andEqualTo("id", id).andEqualTo("delFlag", 0);
        /*Example example = new Example(TypeTemplatePO.class);
        example.and().andEqualTo("delFlag", 0);
        List<TypeTemplatePO> typeTemplatePOS = tempDao.selectByExample(example);
        for (TypeTemplatePO po : typeTemplatePOS) {
            productCodeList.add(po.getTypeId());
        }*/

        //获取平台所有设备
        String deviceType = ApcosApiUtils.getDeviceType();
        //处理成集合
        JSONArray jsonArray = JSONObject.parseArray(deviceType);

        //删除所有分类
        dao.delAllType();

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
            if (!jsonObject1.isEmpty()) {
                String productCode = jsonObject1.get("productCode").toString();
                //如果本地数据库里面没有设备类型就将其【插入】本地数据库
//                if (!productCodeList.contains(productCode)) {
                DeviceTypePO deviceTypePO = JSONObject.parseObject(JSONObject.toJSONString(jsonObject1), DeviceTypePO.class);
                deviceTypePO.preInsert();
                int i1 = dao.insertSelective(deviceTypePO);
                if (jsonObject1.get("mobileTemplate") != null) {
                    TypeTemplatePO po = new TypeTemplatePO();
                    po.preInsert();
                    po.setId(UUID.randomUUID().toString());
//                    po.setTemplateSign(jsonObject1.get("id").toString());
                    po.setTemplateType(2);
                    po.setTypeId(productCode);
                    po.setTemplateIsCommon(true);
                    po.setTemplateCode(jsonObject1.get("mobileTemplate").toString());

                    //查询是否有通用模板，有则更新，无则添加
                    Example example = new Example(TypeTemplatePO.class);
                    example.and().andEqualTo("delFlag", 0).andEqualTo("typeId", productCode)
                            .andEqualTo("templateIsCommon", 1).andEqualTo("templateType", 2);;
                    List<TypeTemplatePO> list = tempDao.selectByExample(example);
                    if (list.size() > 0) {
                        TypeTemplatePO typeTemplatePO = list.get(0);
                        typeTemplatePO.setTemplateCode(jsonObject1.get("mobileTemplate").toString());
                        int i2 = tempDao.updateByPrimaryKeySelective(typeTemplatePO);
                    } else {
                        int i2 = tempDao.insertSelective(po);
                    }
                }
                if (jsonObject1.get("pcTemplate") != null) {
                    TypeTemplatePO po = new TypeTemplatePO();
                    po.preInsert();
                    po.setId(UUID.randomUUID().toString());
//                    po.setTemplateSign(jsonObject1.get("id").toString());
                    po.setTemplateType(1);
                    po.setTypeId(productCode);
                    po.setTemplateIsCommon(true);
                    po.setTemplateCode(jsonObject1.get("pcTemplate").toString());

                    //查询是否有通用模板，有则更新，无则添加
                    Example example = new Example(TypeTemplatePO.class);
                    example.and().andEqualTo("delFlag", 0).andEqualTo("typeId", productCode)
                            .andEqualTo("templateIsCommon", 1).andEqualTo("templateType", 1);
                    List<TypeTemplatePO> list = tempDao.selectByExample(example);
                    if (list.size() > 0) {
                        TypeTemplatePO typeTemplatePO = list.get(0);
                        typeTemplatePO.setTemplateCode(jsonObject1.get("pcTemplate").toString());
                        int i2 = tempDao.updateByPrimaryKeySelective(typeTemplatePO);
                    } else {
                        int i2 = tempDao.insertSelective(po);
                    }
                }
//                }
            }
        }
    }

    @Override
    public void update(DeviceTypeUpdateAlisaVO updateAlisaVO) {
        DeviceTypePO po = new DeviceTypePO();
        BeanUtils.copyProperties(updateAlisaVO, po);
        int i = dao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETUN_SUCCESS) {
            throw new BusinessException(Type.EXCEPTION_FAIL);
        }
    }

    @Override
    public List<DeviceTypeVO> allList() {
        List<DeviceTypeVO> list = dao.allList();
        return list;
    }
}
